package com.luntan.service.impl;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.luntan.domain.MainTie;
import com.luntan.service.MainTieService;
import com.luntan.util.web.Page;

/**
 * 帖、回帖的业务逻辑层（实现类）
 * 
 */
public class MainTieServiceImpl extends BaseServiceImpl<MainTie, Integer> implements MainTieService {

	/**
	 * 保存
	 */
	@Override
	public boolean addSave(MainTie mainTie) {
		boolean flag = false;
		try {
			this.baseDAO.saveEntity(mainTie);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(MainTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}

	/**
	 * 保存修改
	 */
	@Override
	public boolean updateSave(MainTie mainTie) {
		boolean flag = false;
		try {
			
			MainTie temp=this.getEntity(MainTie.class, mainTie.getMid());
			temp.setMtitle(mainTie.getMtitle());
			temp.setMcontent(mainTie.getMcontent());
			
			this.baseDAO.updateEntity(temp);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(MainTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}

	/**
	 * 取出修改
	 */
	@Override
	public MainTie update(Integer id) {
		try {
			MainTie mainTie = this.getEntity(MainTie.class, id);
			return mainTie;
		} catch (Exception ex) {
			Logger.getLogger(MainTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

	/**
	 * 删除
	 */
	@Override
	public boolean delete(Integer id) {
		boolean flag = false;
		try {
			MainTie mainTie = this.getEntity(MainTie.class, id);
			this.baseDAO.removeEntity(mainTie);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(MainTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}

	/**
	 * 带条件获取列表
	 */
	@Override
	public Page<MainTie> getList(int page, int limit, MainTie mainTie) {
		StringBuilder hsql = new StringBuilder();
		StringBuilder where = new StringBuilder(" WHERE 1=1 ").append(this.getWhere(mainTie));
		hsql.append("FROM MainTie AS m ").append(where).append(" ORDER BY m.mdate DESC ");

		try {
			Page<MainTie> list = this.baseDAO.search(hsql.toString(), page, limit);
			return list;
		} catch (Exception ex) {
			Logger.getLogger(MainTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

	/**
	 * 获取列表
	 */
	@Override
	public List<MainTie> getList() {
		StringBuilder hsql = new StringBuilder();
		hsql.append("FROM MainTie AS m ");
		try {
			List<MainTie> list = this.baseDAO.findEntity(hsql.toString());
			return list;
		} catch (Exception ex) {
			ex.printStackTrace();
			Logger.getLogger(MainTie.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

}
